<?php



function encrypt($plaintext){
	global $encryption_key;
	return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $encryption_key, $plaintext, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
}

function decrypt($cypher){
	global $encryption_key;
	return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $encryption_key, base64_decode($cypher), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
}

/**
 * Checks the cookies first. Second it checks the browser default language,
 * if no value is found it returns the default language defined in settings file.
 */
function getDefaultLanguage(){
	
	global $default_language;
	global $languageCookieName;
	
	if(isset($_COOKIE[$languageCookieName])) {
		return $default_language =  $_COOKIE[$languageCookieName];
	}
	setUserLanguageInCookie($default_language);
	return $default_language;
}


function setUserLanguageInCookie($value){
	global $languageCookieName;
	setcookie($languageCookieName, $value, time() + (10 * 365 * 24 * 60 * 60), "/"); // expire in 10 years
}

function getLocalText($key){
	$messages = $_SESSION["messages"];
	if(isset($messages[$key]))
		return  $messages[$key];
	return $key;
}

function setLanguage($choosenLanguage){
	
	if(isset($choosenLanguage) && !empty($choosenLanguage)){
		switch ($choosenLanguage){
			case "en_US":
				$_SESSION["messages"] = parse_ini_file("language/en_US.ini");
				setUserLanguageInCookie($choosenLanguage);
				break;
			case "fr_CA":
				$_SESSION["messages"] = parse_ini_file("language/fr_CA.ini");
				setUserLanguageInCookie($choosenLanguage);
				break;
			default:
				$_SESSION["messages"] = parse_ini_file("language/en_US.ini");
				setUserLanguageInCookie($choosenLanguage);
		}
		return;
	}
}

function loadLanguage(){

	if(!isset($_SESSION["messages"]) || empty($_SESSION["messages"])){
		$lang = getDefaultLanguage();
		switch ($lang){
			case "en_US":
				$_SESSION["messages"] = parse_ini_file("language/en_US.ini");
				break;
			case "fr_CA":
				$_SESSION["messages"] = parse_ini_file("language/fr_CA.ini");
				break;
			default:
				$_SESSION["messages"] = parse_ini_file("language/en_US.ini");
		}
	}

}

function executeQuery($query){
	global $database_url;
	global $database_user;
	global $database_password;
	global $database_name;
	$connexion = new mysqli($database_url, $database_user, $database_password, $database_name);
	if ($connexion->connect_error) {
		die("Connection failed: " . $conn->connect_error);
	}
	$result = $connexion->query($query);
	$connexion->close();
	return $result;
}

function validateEmail($email){
	return !filter_var($email, FILTER_VALIDATE_EMAIL);
}

function isAdmin(){
	if(isset($_SESSION["role"]) && $_SESSION["role"]==1 )
		return true;
	return false;
}

function generatePassword(){
	$chars = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
	$password="";
	for ($i = 0; $i < 8; $i++) {
		$n = rand(0, strlen($chars)-1);
		$password .= $chars[$n];
	}
	return $password;
}

function notifyNewUser($firstName,$lastName,$email,$password){
	error_log("New user $email , password : ".$password,0);
}

function notifyNewPassword($firstName,$lastName,$email,$password){
	error_log("Password changed for $email , new password : ".$password,0);
}



?>
